package primetest.impl.dao;

import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import java.util.List;


import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import primetest.dto.BuildingDto;
import primetest.impl.dao.BuildingDao;
import primetest.impl.dao.Transaction;

//@Stateless

public class BuildingDao implements Serializable {
	/**
	 *
	 */
	private static final long serialVersionUID = 6551770662358620809L;

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	private static List<BuildingDto> allItems = null;
	private static HashMap<String, BuildingDto> map = null;

	@Inject
	public BuildingDao() {
	}

	public List<BuildingDto> getAllList(Transaction transaction)
			throws IOException {
		if (allItems != null) {
			return allItems;
		}
		PreparedStatement prepareStatement = null;
		ResultSet resultSet = null;
		map = new HashMap<String, BuildingDto>();
		try {

			Connection connection = transaction.getResource(Connection.class);
			prepareStatement = connection.prepareStatement(GET_ALL);
			resultSet = prepareStatement.executeQuery();

			while (resultSet.next()) {
				BuildingDto buildingDto = makeBuildingDto(transaction,
						resultSet);
				map.put(buildingDto.getKey(), buildingDto);
			}
			allItems = new ArrayList<BuildingDto>(map.values());
			return allItems;
		} catch (SQLException e) {
			throw new IOException(e);
		} finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}

			if (prepareStatement != null) {
				try {
					prepareStatement.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}
		}
	}

	private BuildingDto makeBuildingDto(Transaction transaction,
			ResultSet resultSet) throws SQLException, IOException {
		BuildingDto buildingDto = new BuildingDto(resultSet.getString(1),
				resultSet.getString(2), resultSet.getInt(3));
		return buildingDto;
	}

	public void addBuilding(Transaction transaction, BuildingDto buildingDto)
			throws IOException {
		PreparedStatement prepareStatement = null;
		ResultSet resultSet = null;

		try {
			Connection connection = transaction.getResource(Connection.class);
			prepareStatement = connection.prepareStatement(INSERT_ONE);
			prepareStatement.setString(1, buildingDto.getKey());
			prepareStatement.setString(2, buildingDto.getName());
			prepareStatement.setInt(3, buildingDto.getFloorCount());
			prepareStatement.executeUpdate();
			map.put(buildingDto.getKey(), buildingDto);
			allItems.add(buildingDto);
		} catch (SQLException e) {
			throw new IOException(e);
		} finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}

			if (prepareStatement != null) {
				try {
					prepareStatement.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}
		}
	}

	public void updateBuilding(Transaction transaction, BuildingDto buildingDto)
			throws IOException {
		PreparedStatement prepareStatement = null;
		ResultSet resultSet = null;

		try {
			Connection connection = transaction.getResource(Connection.class);
			prepareStatement = connection.prepareStatement(UPDATE_ONE);
			prepareStatement.setString(3, buildingDto.getKey());
			prepareStatement.setString(1, buildingDto.getName());
			prepareStatement.setInt(2, buildingDto.getFloorCount());
			prepareStatement.executeUpdate();
			map.put(buildingDto.getKey(), buildingDto);
		} catch (SQLException e) {
			throw new IOException(e);
		} finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}

			if (prepareStatement != null) {
				try {
					prepareStatement.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}
		}

	}

	public BuildingDto findBuilding(Transaction transaction, String key) throws IOException {
		if(allItems == null){
			getAllList(transaction);
		}
		return map.get(key);
	}

	public void deleteBuilding(Transaction transaction, String buildingSeq)
			throws IOException {
		PreparedStatement prepareStatement = null;
		ResultSet resultSet = null;

		try {
			Connection connection = transaction.getResource(Connection.class);
			prepareStatement = connection.prepareStatement(DELETE_ONE);
			prepareStatement.setString(1, buildingSeq);
			prepareStatement.executeUpdate();
			allItems.remove(map.remove(buildingSeq));
		} catch (SQLException e) {
			throw new IOException(e);
		} finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}

			if (prepareStatement != null) {
				try {
					prepareStatement.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}
		}

	}

	private static final String GET_ALL = "select key_1, name, floor_count from s_building";
	private static final String INSERT_ONE = "insert into s_building(key_1, name, floor_count) values (?, ?, ?)";
	private static final String UPDATE_ONE = "update s_building set name = ?, floor_count = ? where key_1 = ?";
	private static final String DELETE_ONE = "delete from s_building where key_1 = ?";
}
